﻿using ai_library;
using UnityEngine;

public class AiActorBehavior : MonoBehaviour {
	
	public IEntity Entity
	{
		get;
		private set;
	}

	public bool SteeringHelpersOn = false;

	public bool FeelerHelpersOn = false;

	public bool WanderOn = false;

	public bool WallAvoidOn = false;

	public bool ObstacleAvoidOn = false;
	
	public AiActorBehavior PursuitActor = null;
	public AiActorBehavior EvadeActor = null;

	void Awake()
	{
		Entity = null;
	}

	void Start () 
	{
		Transform trans = gameObject.transform;
		
		try
		{
			Entity = EntityFactory.Get(EntityTypes.Agent, trans);
		}
		catch (EntityFactoryException e)
		{
			Debug.Log (e.ToString());
		}

		if (EntityManager.Instance.IsAgentRegistered(Entity))
		{
			Debug.Log("Entity " + Entity.ID + "already registered to EntityManager?");
		}
		else
		{
			EntityManager.Instance.RegisterAgent(Entity);
		}
	}

	void Update () 
	{
		try
		{
			// Helpers
			if (SteeringHelpersOn != Entity.EnableDebugSteeringHelpers)
			{
				Entity.EnableDebugSteeringHelpers = !Entity.EnableDebugSteeringHelpers;
			}

			if (FeelerHelpersOn != Entity.EnableDebugFeelerHelpers)
			{
				Entity.EnableDebugFeelerHelpers = !Entity.EnableDebugFeelerHelpers;
			}

			//Walls
			if (WallAvoidOn && !Entity.Steering.isWallAvoidanceOn())
			{
				Entity.Velocity = Vector2.zero;
				Entity.Steering.WallAvoidanceOn();
			}
			else if (!WallAvoidOn && Entity.Steering.isWallAvoidanceOn())
			{
				Entity.Velocity = Vector2.zero;
				Entity.Steering.WallAvoidanceOff();
			}

			//Obstacles
			if (ObstacleAvoidOn && !Entity.Steering.isObstacleAvoidanceOn())
			{
				Entity.Velocity = Vector2.zero;
				Entity.Steering.ObstacleAvoidanceOn();
			}
			else if (!ObstacleAvoidOn && Entity.Steering.isObstacleAvoidanceOn())
			{
				Entity.Velocity = Vector2.zero;
				Entity.Steering.ObstacleAvoidanceOff();
			}

			// Wander
			if (WanderOn && !Entity.Steering.isWanderOn())
			{
				Entity.Velocity = Vector2.zero;
				Entity.Steering.WanderOn();
			}
			else if (!WanderOn && Entity.Steering.isWanderOn())
			{
				Entity.Velocity = Vector2.zero;
				Entity.Steering.WanderOff();
			}

			//Persuit
			if (PursuitActor != null)
			{
				if (PursuitActor.Entity != null && !Entity.Steering.isPursuitOn())
				{
					Entity.Steering.PursuitOn(PursuitActor.Entity);
				}
				else if (PursuitActor.Entity == null && Entity.Steering.isPursuitOn())
				{
					Entity.Steering.PursuitOff();
				}
			}

			//Evade
			if (EvadeActor != null)
			{
				if (EvadeActor.Entity != null && !Entity.Steering.isEvadeOn())
				{
					Entity.Steering.EvadeOn(EvadeActor.Entity);
				}
				else if (EvadeActor.Entity == null && Entity.Steering.isEvadeOn())
				{
					Entity.Steering.EvadeOff();
				}
			}

			Entity.Update(Time.deltaTime);
		}
		catch(EntityException e)
		{
			Debug.Log(e.ToString());
		}
	}

	void OnDestroy()
	{
		if (EntityManager.Instance.IsAgentRegistered(Entity))
		{
			EntityManager.Instance.RemoveAgent(Entity);
		}
		else
		{
			Debug.Log("Agent " + Entity.ID + "isn't registered to EntityManager?");
		}
	}
}
